package com.hxd.tabactivityfragment.ui.fragment

import android.app.Activity.RESULT_OK
import com.hxd.tabactivityfragment.R
import com.hxd.tabactivityfragment.common.MyFragment
import kotlinx.android.synthetic.main.fragment_find.*
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.view.Menu
import android.view.MenuInflater
import com.miguelcatalan.materialsearchview.MaterialSearchView
import android.text.TextUtils
import android.speech.RecognizerIntent
import android.content.Intent
import android.os.Handler
import com.hxd.tabactivityfragment.ui.activity.DataExhibitionActivity
import com.hxd.tabactivityfragment.util.GsonToBean
import com.hxd.tabactivityfragment.util.L
import com.hxd.tabactivityfragment.util.URLProviderUtils
import com.kymjs.rxvolley.RxVolley
import com.kymjs.rxvolley.client.HttpCallback
import com.kymjs.rxvolley.client.HttpParams
import org.jetbrains.anko.support.v4.startActivity


/**
 *  项目名：    Tab+Activity+Fragment
 *  包名：      com.hxd.tabactivityfragment.ui.fragment
 *  文件名:     HomeFragment
 *  创建者:     HXD
 *  创建时间:   2019/3/7 15:51
 *  描述:       发现&查询界面
 */

class FindFragment : MyFragment() {

    //设置单例静态方法
    companion object {
        fun newInstance(): FindFragment {
            return FindFragment()
        }
    }

    override fun getTitleBarId(): Int {
        return R.id.tb_find_title
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_find
    }

    override fun init() {
        //显示右上角的查询按钮
        setHasOptionsMenu(true)
    }

    override fun initView() {
        super.initView()
        (activity as AppCompatActivity).setSupportActionBar(toolbar as Toolbar)

        //是否开启语音搜索
        search_view.setVoiceSearch(false)
        search_view.setCursorDrawable(R.drawable.color_cursor_white)
        search_view.setEllipsize(true)
        //设置查询字典
        search_view.setSuggestions(resources.getStringArray(R.array.query_suggestions))
    }

    override fun initListener() {
        search_view.setOnQueryTextListener(object : MaterialSearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                toast("您查询了: $query")
                var params = HttpParams()
                params.putJsonParams(GsonToBean.QueryToJson(query!!))
                RxVolley.jsonPost(URLProviderUtils.getIMPSearchUrl(),params, object : HttpCallback() {
                    override fun onSuccess(result: String?) {
                        L.i("FindFragment : $result")
                        rxDialogShapeLoading!!.show()
                        Handler().postDelayed({
                            rxDialogShapeLoading!!.dismiss()
                            startActivity<DataExhibitionActivity>("result" to result!!)
                        }, 1500)
                    }

                    override fun onFailure(errorNo: Int, strMsg: String?) {
                        L.i("FindFragment : $strMsg")
                    }
                })
                search_view.closeSearch()
                return true
            }

            //在这里初始化历史列表
            override fun onQueryTextChange(newText: String?): Boolean {
                return true
            }
        })

        search_view.setOnSearchViewListener(object : MaterialSearchView.SearchViewListener {
            override fun onSearchViewClosed() {

            }

            override fun onSearchViewShown() {

            }

        })
    }

    //使用沉浸式状态栏
    override fun isStatusBarEnabled(): Boolean {
        return !super.isStatusBarEnabled()
    }

    //绑定search_view和toolbar
    override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
        activity!!.menuInflater.inflate(R.menu.search, menu)
        search_view.setMenuItem(menu?.findItem(R.id.action_search))
    }

    //查询结果回调
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == MaterialSearchView.REQUEST_VOICE && resultCode == RESULT_OK) {
            val matches = data!!.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
            if (matches != null && matches.size > 0) {
                val searchWrd = matches[0]
                if (!TextUtils.isEmpty(searchWrd)) {
                    search_view.setQuery(searchWrd, false)
                }
            }
            return
        }
        super.onActivityResult(requestCode, resultCode, data)
    }
}